# 平衡二叉树
# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        return self.getDept(root) != -1

    def getDept(self, root: TreeNode):
        if root is None:
            return 0
        left = self.getDept(root.left)
        right = self.getDept(root.right)
        if right == -1 or left == -1 or abs(right - left) > 1:
            return -1
        return max(left, right) + 1


if __name__ == '__main__':
    so = Solution()
    root = TreeNode(1)
    root.left = TreeNode(0)
    root.left.right = TreeNode(-5)
    # root.right = TreeNode(10)
    r = so.isBalanced(root)
    print(r)
